<!-- 编写组件-->
<template>
  <div class="my-image">
    <div class="btn-image" style="margin-top:20px" @click="openDialog">
      <!-- 短路运算 如果value有值就使用value值 反之使用previewImg的值-->
      <img :src="value || previewImg" alt />
    </div>
    <!-- 对话框 -->
    <el-dialog :visible.sync="dialogVisible" width="720px">
      <!-- tabs组件 v-model="activeName" 激活的选项卡的name属性值-->
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="素材库" name="images">
          <!-- 按钮 全部/收藏-->
          <el-radio-group @change="toggleList()" v-model="reqParams.collect" size="small">
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>
          </el-radio-group>
          <!-- 素材列表-->
          <div class="img-list" style="margin-top:15px">
            <div
              :class="{'selected':item.url===selectedImgUrl}"
              @click="selectedImg(item.url)"
              class="img-item"
              v-for="item in images"
              :key="item.id"
            >
              <img :src="item.url" alt />
            </div>
          </div>
          <!-- 分页列表-->
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :count-page="reqParams.page"
            :page-size="reqParams.per_page"
            @current-change="changePager"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="上传图片" name="upload">
          <el-upload
            class="avatar-uploader"
            action="http://ttapi.research.itcast.cn/mp/v1_0/user/images"
            :show-file-list="false"
            :headers="headers"
            name="image"
            :on-success="uploadSuccess"
          >
            <img v-if="uploadImgUrl" :src="uploadImgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmImg">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import auth from "@/utils/auth";
// 导入预览图片路径
import img from "@/assets/default.png";
export default {
  name: "my-image",
  props: ["value"],
  data() {
    return {
      // 封面预览
      previewImg: img,
      // 当前图片上传地址
      uploadImgUrl: null,
      // 请求头数据
      headers: { Authorization: `Bearer ${auth.getUser().token}` },
      dialogVisible: false,
      activeName: "images",
      // 控制显示与隐藏
      reqParams: {
        collect: false,
        page: 1,
        per_page: 8
      },
      // 素材列表
      images: [],
      // 总条数
      total: 0,
      // 当前选中图片的url
      selectedImgUrl: null
    };
  },
  methods: {
    // 打开对话框
    openDialog() {
      this.dialogVisible = true;
      this.getImages();
      // 打开对话框  要清除上一次的记录
      // 设置为素材库
      this.activeName = "images";
      // 清除选中图片地址
      this.selectedImgUrl = null;
      // 清除上传图片的地址
      this.uploadImgUrl = null;
    },
    // 获取图片素材数据
    async getImages() {
      const res = await this.$http.get("user/images", {
        params: this.reqParams
      });
      // 获取数据后赋值给images数组
      this.images = res.data.data.results;
      // 获取总条数
      this.total = res.data.data.total_count;
    },
    // 进行分页
    changePager(newPage) {
      this.reqParams.page = newPage;
      // 渲染列表
      this.getImages();
    },
    // 切换全部和收藏功能
    toggleList() {
      // 显示第一列
      this.reqParams.page = 1;
      // 渲染列表
      this.getImages();
    },
    // 获取图片url
    selectedImg(url) {
      this.selectedImgUrl = url;
    },
    // 上传图片成功函数
    uploadSuccess(response) {
      this.$message.success("上传成功");
      this.uploadImgUrl = response.data.url;
    },
    // 确认按钮事件
    confirmImg() {
      // 判断  如果激活的是素材库选项框
      if (this.activeName === "images") {
        // 激活素材库  如果没有选中图片
        if (!this.selectedImgUrl)
          return this.$message.warning("请选择一张图片");
        // 预览图片地址 换成选中图片地址
        // this.previewImg = this.selectedImgUrl;
        // 子传父  将选中的图片地址传给父组件使用 也就是传给publish/index.vue
        this.$emit("input", this.selectedImgUrl);
      } else {
        //激活的上传图片选项框
        if (!this.uploadImgUrl) return this.$message.warning("请上传一张图片");
        // 预览图片地址 换成上传图片地址
        // this.previewImg = this.uploadImgUrl;
        // 子传父  将上传的的图片地址传给父组件使用 也就是传给publish/index.vue
        this.$emit("input", this.uploadImgUrl);
      }
      // 关闭对话框
      this.dialogVisible = false;
      // 在确认图片后，会触发my-image组件的自定义事件confirm
      this.$emit("confirm");
    }
  }
};
</script>
 <style lang='less' scoped>
.my-image {
  display: inline-block;
  margin-right: 20px;
  .btn-image {
    width: 150px;
    height: 150px;
    border: 1px dashed #ddd;
  }

  img {
    display: block;
    width: 100%;
    height: 100%;
  }
  .img-list {
    .img-item {
      display: inline-block;
      width: 150px;
      height: 120px;
      border: 1px dashed #ddd;
      margin-right: 15px;
      position: relative;
      &.selected::after {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.3) url(../assets/selected.png) no-repeat
          center / 50px auto;
      }
    }
  }
}
</style>